<template>
  <div class="app-container">
    <el-form :inline="true" :model="dataForm" ref="dataForm">
      <el-form-item label="资源名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="请输入资源名称"></el-input>
      </el-form-item>
      <el-form-item label="课程名称" prop="courseName">
        <el-input v-model="dataForm.courseName" placeholder="请输入课程名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="getDataList()">查询</el-button>
        <el-button icon="el-icon-refresh-left" @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="avue-crud">
      <el-table
          :data="dataList"
          border
          v-loading="dataListLoading">
<!--        <el-table-column-->
<!--            prop="courseName"-->
<!--            header-align="center"-->
<!--            align="center"-->
<!--            label="科目名称">-->
<!--        </el-table-column>-->
        <el-table-column
            prop="type"
            header-align="center"
            align="center"
            label="资源类型">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.type === '2'">文档</el-tag>
            <el-tag v-if="scope.row.type === '1'" type="danger">视频</el-tag>
            <el-tag v-if="scope.row.type === '3'" type="danger">在线课程</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            prop="name"
            header-align="center"
            align="center"
            label="资源名称">
        </el-table-column>
        <el-table-column
            prop="status"
            header-align="center"
            align="center"
            label="资源状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === 0">待审核</el-tag>
            <el-tag v-if="scope.row.status === 1" type="danger">审核失败</el-tag>
            <el-tag v-if="scope.row.status === 2" type="info">审核通过</el-tag>
            <el-tag v-if="scope.row.status === 3" type="warning">下架</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            prop="createTime"
            header-align="center"
            align="center"
            label="创建时间">
        </el-table-column>
        <el-table-column
            prop="updateTime"
            header-align="center"
            align="center"
            label="更新时间">
        </el-table-column>
        <el-table-column
            header-align="center"
            align="center"
            label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" icon="el-icon-edit" @click="viewHandle(scope.row)">学习</el-button>
            <el-button type="text" size="small" icon="el-icon-edit" @click="favoriteHandle(scope.row.id)">收藏</el-button>
            <el-button type="text" size="small" icon="el-icon-edit" @click="addPj(scope.row.id)">评价</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="avue-crud__pagination">
      <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="pageSize"
          :total="totalPage"
          background
          layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
    <div>
      <el-dialog
          title="观看视频"
          :visible="dialogVisible"
          width="70%"
          :before-close="handleClose">
        <div v-if="dialogVisible">
          <video style="width: 100%; height: 550px" ref="videoPlayer" class="video-js"></video>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="handleClose">关 闭</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {fetchEduList, delObj} from '@/api/teacher/resource'
import {addObj} from '@/api/student/resourcefavorite'
import TableForm from './resource-form'
import store from "@/store";
import videojs from "video.js"
import "video.js/dist/video-js.css"

export default {
  data() {
    return {
      player: null,
      options: {
        // Video.js 配置选项
        // controls: true,
        // autoplay: true,
        // preload: 'auto'
        // 其他配置选项...
        autoplay: false,
        controls: true,
        preload: 'auto',
        fluid: true,
        aspectRatio: '16:9',
        html5: {
          hls: {
            enableLowInitialPlaylist: true,
            overrideNative: true
          }
        }
      },
      dialogVisible: false,
      dataForm: {
        userId: store.getters.token.split(",")[2],
        name: ''
      },
      searchForm: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 5,
      totalPage: 0,
      dataListLoading: false,
      addOrUpdateVisible: false
    }
  },
  components: {
    TableForm
  },
  mounted() {
  },
  created() {
    this.getDataList()
  },
  methods: {
    resetForm() {
      this.$refs['dataForm'].resetFields()
      this.getDataList()
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      fetchEduList(Object.assign({
        current: this.pageIndex,
        size: this.pageSize
      }, this.dataForm)).then(response => {
        this.dataList = response.data.records
        this.totalPage = response.data.total
      })
      this.dataListLoading = false
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    viewHandle(row) {
      if (row.type === '2') {
        console.log(row.url)
        window.open(row.url, '_blank');
      } else {
        this.dialogVisible = true
        this.$nextTick(() => {
          this.initVideoPlayer(row.url)
        })
      }
    },
    favoriteHandle(id) {
      addObj({resourceId: id}).then(response => {
        this.$message.success('收藏成功！')
      })
    },
    initVideoPlayer(url) {
      // 初始化视频播放器
      this.player = videojs(this.$refs.videoPlayer, {
        ...this.options,
        sources: [{
          src: url,
          type: 'application/x-mpegURL'
        }]
      }, function onPlayerReady() {
        console.log('Video.js player is ready');
      });
    },
    handleDialogClose() {
      // 关闭对话框时停止视频播放并销毁播放器
      if (this.player) {
        this.player.pause(); // 暂停视频播放
        this.player.dispose(); // 销毁播放器以释放资源
        this.player = null; // 置空播放器对象
      }
    },
    handleClose() {
      this.handleDialogClose()
      this.dialogVisible = false
    },
    // 删除
    deleteHandle(id) {
      this.$confirm('是否确认删除ID为' + id, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delObj(id)
      }).then(data => {
        this.$message.success('删除成功')
        this.getDataList()
      })
    },
    // 新增 / 修改
    addPj(id) {
      this.$router.push({ path: '/pj', query: { id: id }});
    }
  }
}
</script>
